<!-- 新增收货地址 -->
<template>
  <view>
    <view class="module">
      <view class="row">
        <view class="label">
          收货人：
        </view>
        <input type="text" v-model="params.name" placeholder="请输入收货人姓名" placeholder-class="placeholder" class="input" />
      </view>
      <view class="row">
        <view class="label">
          手机号：
        </view>
        <input type="text" v-model="params.phone" placeholder="请输入收货人手机号" placeholder-class="placeholder" class="input" />
      </view>
      <view class="row">
        <view class="label">
          收货地址：
        </view>
        <textarea type="text" v-model="params.address" placeholder="请输入收货地址" placeholder-class="placeholder" class="textarea" />
      </view>
    </view>

    <view class="bottom-row" @tap="selected">
      <view class="select-btn">
        <image :src="params.is_default == 1 ? '../../static/select-pink.png' : '../../static/select.png'" />
      </view>
      设为默认地址
    </view>

    <view class="btn-box">
      <button v-if="params.id == 0" type="default" @tap="submit()">确 认</button>
      <!-- 编辑地址的时候显示 -->
      <button v-if="params.id > 0" type="default" @tap="submit()">确认修改</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      params: {
        id: 0,
        name: '', // 收货人姓名
        phone: '', // 手机号码
        address: '', // 具体楼牌门号
        is_default: 0, //  1默认 0非默认
      },

    }
  },
  onLoad(option) {
    // 获取参数
    const params = uni.getStorageSync('page_params_address_add_detail') || this.params;
    uni.removeStorageSync('page_params_address_add_detail');
    this.params = params;
    uni.setNavigationBarTitle({
      title: this.params.id > 0 ? '编辑收货地址' : '新增收货地址',
    });
  },
  onShow() {
  },

  methods: {
    // 新增地址 or 编辑地址
    submit(){
      const params = this.params;
      if (params.name === '') {
        return uni.showToast({
          icon: 'none',
          title: '请输入收货人姓名',
        });
      }
      if (params.phone === '') {
        return uni.showToast({
          icon: 'none',
          title: '请输入收货人手机号',
        });
      }
      if (params.address === '') {
        return uni.showToast({
          icon: 'none',
          title: '请输入收货地址',
        });
      }
      // 判断“新增 or 编辑”地址
      const path = params.id > 0 ? 'api/newapi/saveUserAddress' : 'api/newapi/addUserAddress';
      this.$http(path, params).then(res => {
        uni.showToast({
          icon: 'none',
          title: params.id > 0 ? '更新成功' : '新增成功',
        })
        setTimeout(() => {
          uni.navigateBack();
        }, 1000)
      });
    },
    //设为默认地址
    selected() {
      this.params.is_default = this.params.is_default == 1 ? 0 : 1;
    },
  }
}
</script>

<style>
page {
  background: #f5f5f5;
}
</style>
<style scoped lang="scss">
 page{
   background: #f5f5f5;
 }

.bottom-row {
  width: calc(100% - 56rpx);
  margin: 25rpx auto;
  display: flex;
  align-items: center;
  color: #3c3c3c;
  font-size: 24rpx;
  line-height: 24rpx;
}

.select-btn {
  display: flex;
  align-items: center;
  border-radius: initial;
  background-color: initial;
  margin-right: 16rpx;
}

.select-btn image {
  width: 28rpx;
  height: 28rpx;
  filter: brightness(0.2);
}

.module {
  width: calc(100% - 40rpx);
  margin: 30rpx auto 0;
  background-color: #fff;
  border-radius: 20rpx;
  display: flex;
  flex-direction: column;
  padding: 0 20rpx;
  box-sizing: border-box;
  box-shadow: 0 0 16rpx rgba(0, 0, 0, 0.08);
}

.row {
  width: 100%;
  border-bottom: 1px solid #efefef80;
  box-sizing: border-box;
  display: flex;
  //align-items: center;
  .label{
    width: 150rpx;
    height: 92rpx;
    line-height: 92rpx;
    color: #3c3c3c;
    text-align: right;
  }
  &:last-child{
    border: none;
  }
}
 .textarea {
   height: 160rpx;
   flex: 1;
   color: #282828;
   font-size: 24rpx;
   padding: 33rpx 0;
 }

.input {
  height: 92rpx;
  line-height: 92rpx;
  color: #282828;
  font-size: 24rpx;
  flex: 1;
}

.placeholder {
  color: #999999;
}

.btn-box {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 60rpx 75rpx;
  box-sizing: border-box;
}

.btn-box button {
  width: 100%;
  height: 100rpx;
  line-height: 100rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #f5f5f5;
  font-size: 34rpx;
  font-weight: 500;
  background: linear-gradient(90deg, #EEA634, #DE2D32);
  border-radius: 100rpx;
  margin-bottom: 18rpx;
}

.gray-bg {
  background: #f5f5f5 !important;
  color: #7d7d7d !important;
}

.phone-icon {
  width: 35rpx;
}
</style>
